package com.fyb;


import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.style.Style0;
import org.apache.commons.collections.MapUtils;

/**
 * @author 作者 : YH
 * @version 1.0
 * @date 创建时间： 2020/5/9 12:50
 * @description ${description}
 */
public class GeTuiUtil {


    public static Boolean PushData(Integer pushPlatform, String cid, String title, String text, Boolean rain, Boolean vibrate) {
        try {
            IGtPush push = new IGtPush(BaseProperties.GE_TUI_URL, BaseProperties.GE_TUI_APP_KEY, BaseProperties.GE_TUI_MASTER_SECRET);


            SingleMessage message = new SingleMessage();
            NotificationTemplate template = new NotificationTemplate();
            // 设置APPID与APPKEY
            template.setAppId(BaseProperties.GE_TUI_APPID);
            template.setAppkey(BaseProperties.GE_TUI_APP_KEY);

//            if (pushPlatform == 1) {
                Style0 style = new Style0();
                // 设置通知栏标题与内容
                style.setTitle(title);
                style.setText(text);
                // 配置通知栏图标
//            style.setLogo("icon.png");
                // 配置通知栏网络图标
//            style.setLogoUrl("");
                // 设置通知是否响铃，震动，或者可清除
                style.setRing(rain);
                style.setVibrate(vibrate);
                style.setClearable(true);
                template.setStyle(style);
//            }
            if (pushPlatform == 2) {
                template.setAPNInfo(getAPNPayload(title, text)); //详见【推送模板说明】iOS通知样式设置
            }
            message.setData(template);
            message.setOffline(true);
            message.setOfflineExpireTime(6000 * 1000);

            // 设置推送目标，填入appid和clientId
            Target target = new Target();
            target.setAppId(BaseProperties.GE_TUI_APPID);
            target.setClientId(cid);
            // STEP6：执行推送
            IPushResult ret = push.pushMessageToSingle(message, target);
            if ("ok".equals(MapUtils.getString(ret.getResponse(), "result"))) {
                return true;
            }

        } catch (Exception e) {
            System.out.println(e);
        }
        return false;
    }

    private static APNPayload getAPNPayload(String title, String text) {
        APNPayload payload = new APNPayload();
        //在已有数字基础上加1显示，设置为-1时，在已有数字上减1显示，设置为数字时，显示指定数字
        payload.setAutoBadge("+1");
//        payload.setContentAvailable(0);
        //ios 12.0 以上可以使用 Dictionary 类型的 sound
        payload.setSound("default");
//        payload.setCategory("$由客户端定义");
//        payload.addCustomMsg("由客户自定义消息key", "由客户自定义消息value");

        //简单模式APNPayload.SimpleMsg
//        payload.setAlertMsg(new APNPayload.SimpleAlertMsg("hello"));
        payload.setAlertMsg(getDictionaryAlertMsg(title, text));  //字典模式使用APNPayload.DictionaryAlertMsg

        return payload;
    }

    private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(String title, String text) {
        APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
        alertMsg.setBody(text);
//        alertMsg.setActionLocKey("显示关闭和查看两个按钮的消息");
//        alertMsg.setLocKey("loc-key1");
//        alertMsg.addLocArg("loc-ary1");
//        alertMsg.setLaunchImage("调用已经在应用程序中绑定的图形文件名");
        // iOS8.2以上版本支持
        alertMsg.setTitle(title);
//        alertMsg.setTitleLocKey("自定义通知标题");
//        alertMsg.addTitleLocArg("自定义通知标题组");
        return alertMsg;
    }
}
